//
//  AuthenticationViewController.m
//  TQY
//
//  Created by 甘世清 on 2024/11/14.
//

#import "AuthenticationViewController.h"
#import "AuthenticationPopView.h"
@interface AuthenticationViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic , strong) SQCustomButton * addButton;
@property (nonatomic, strong) UIImagePickerController *imagePickerVc;
@property (nonatomic, strong) NSString * imageUrl;
@property (nonatomic , strong) HeaderView * addbtn ;
@end

@implementation AuthenticationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"认证");
    WS(weakSelf);
    if (self.isPush) {
        self.zx_handlePopBlock = ^BOOL(ZXNavigationBarController * _Nonnull viewController, ZXNavPopBlockFrom popBlockFrom) {
            return NO;
        };
        self.zx_navLeftBtn.hidden = YES;
        [self zx_setRightBtnWithText:SQNSLocalString(@"跳过") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
            [weakSelf SQ_popViewVC];
        }];
        [self SQ_Header];
    }
}
- (void)setIsPush:(BOOL)isPush{
    _isPush = isPush;
  
}
- (void)SQ_UIViewForSelfView{
    UIImageView * bgImageView = [[UIImageView alloc]init];
    bgImageView.image = SQGetImage(@"二级背景");
    [self.view addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self.view addSubview:self.addButton];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(140);
        make.height.mas_equalTo(210);
        make.right.equalTo(self.view.mas_right).offset(-30);
        make.top.equalTo(self.zx_navBar.mas_bottom).offset(20);
    }];
    
    SQCustomButton* shili = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(130, 170) midmargin:20];
    shili.image = SQGetImage(@"示例");
    shili.imageView.contentMode = UIViewContentModeScaleAspectFill;
    shili.SQBackimageView.contentMode = UIViewContentModeScaleAspectFill;
    shili.imageView.clipsToBounds = YES;
    shili.imageView.layer.cornerRadius = 10;
    shili.imageView.layer.masksToBounds = YES;
    shili.text = SQNSLocalString(@"认证示例照片");
    shili.font = FONT_REGULAR(12);
    shili.textColor = SQColor999999;
    [self.view addSubview:shili];
    [shili mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(140);
        make.height.mas_equalTo(210);
        make.left.equalTo(self.view).offset(30);
        make.top.equalTo(self.zx_navBar.mas_bottom).offset(20);
    }];
    
    
    self.addbtn = [[HeaderView alloc]init];
    self.addbtn.text = SQNSLocalString(@"开始认证");
    self.addbtn.font = FONT_SEMIBOLD(16);
    [self.addbtn.btn addTarget:self action:@selector(SQ_UploadeImage) forControlEvents:UIControlEventTouchUpInside];
    self.addbtn.colorString = @"#FFFFFF";
    self.addbtn.image = @"bg矩形";
    self.addbtn.layer.cornerRadius = 25;
    self.addbtn.layer.masksToBounds = YES;
    self.addbtn.hidden = YES;
    [self.view addSubview: self.addbtn];
    [ self.addbtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(SCREEN_WIDETH-70);
            make.height.mas_equalTo(50);
            make.centerX.equalTo(self.view);
            make.top.equalTo(self.addButton.mas_bottom).offset(100);
    }];
   
    
    YYLabel * label3 = [[YYLabel alloc]initWithFrame:CGRectMake(40, SCREEN_HEIGHT- FootHeight - 80, SCREEN_WIDETH-80, 70)];
    label3.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    label3.textAlignment = NSTextAlignmentCenter;
    label3.numberOfLines = 0;
    [self.view addSubview:label3];
    NSDictionary *attributes = @{NSFontAttributeName:FONT_MEDIUM(12) , NSForegroundColorAttributeName: SQColor(@"#333333")};
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:SQNSLocalString(@"  进行认证则默认同意《隐私协议》,按要求拍摄并上传个人真实有效照片,用以完成真人认证,我们将严格进行保密并保证仅用于认证使用") attributes:attributes];
    WS(weakSelf);
    //设置高亮色和点击事件
    [text yy_setTextHighlightRange:[[text string] rangeOfString:SQNSLocalString(@"《隐私协议》")] color:SQColor3DA59D backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        CommonWebVcView *vc= [[CommonWebVcView alloc]init];
        vc.urlStr =  @"https://taoqingyan.qingtao.fun/agreement.html?id=2";
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    NSRange range1=[[text string]rangeOfString:SQNSLocalString(@"《隐私协议》")];
    YYTextDecoration* deco=[YYTextDecoration decorationWithStyle:(YYTextLineStyleSingle) width:[NSNumber numberWithInt:0] color:[UIColor whiteColor]];
    [text yy_setTextUnderline:deco range:range1];
    label3.attributedText = text;
    [self.addButton setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf presentViewController:weakSelf.imagePickerVc animated:YES completion:nil];
    }];
}
-(void)SQ_UploadeImage{
    self.addbtn.hidden = YES;
    if ([self.imageUrl isNotEmpty]) {
        [self SQ_addButtonClick];
    }else{
        [YJProgressHUD SQ_showMsgWithoutView:@""];
        WS(weakSelf);
        [HttpTool SQ_Image:self.addButton.image successBlock:^(BaseModel * _Nonnull httpModel) {
            self.imageUrl = [NSString stringWithFormat:@"%@",httpModel.data];
            [weakSelf SQ_addButtonClick];
        } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
            [YJProgressHUD showMsgWithoutView:httpModel.message];
        }];
    }
}
-(void)SQ_addButtonClick{
    AuthenticationPopView * vc = [[AuthenticationPopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT)];
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                           parentView:self.view
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleBottom;
    pop.isAvoidKeyboard = NO;
    [pop pop];
    LSTPopViewWK(pop)
    WS(weakSelf);
    [vc.deteleBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [wk_pop dismiss];
        weakSelf.addbtn.hidden = NO;
    }];
  
    [vc setTicketBlock:^(NSString * _Nonnull urlString) {
        [wk_pop dismiss];
        if ([urlString isNotEmpty]) {
            [weakSelf SQ_UploadeVideo:urlString];
        } 
    }];
}
-(void)SQ_UploadeVideo:(NSString*)urlString{
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.imageUrl forKey:@"portrait"];
    [dic setObject:urlString forKey:@"url"];
    [HttpTool postJson:@"realAuth/save" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        UserModel * model = [UserModel share];
        model.realStatus = 0;
        [UserModel updateLocalUserModel:model];
        [self SQ_popViewVC];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"认证资料提交成功,我们将在24小时内审核完成!")];
        });
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
        self.addbtn.hidden = NO;
    }];
}

- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"face_switch" forKey:@"key"];
    [HttpTool post:@"config/details" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSString * value = [NSString stringWithFormat:@"%@",[data objectForKey:@"value"]];
        if ([value isEqualToString:@"1"]) {
            self.zx_navRightBtn .hidden = YES;
        } else {
            self.zx_navRightBtn .hidden = NO;
        }
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}


- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    WS(weakSelf);
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        UIImage *photo = [info objectForKey:UIImagePickerControllerEditedImage];
        if (photo == nil)
        {
            photo = [info objectForKey:UIImagePickerControllerOriginalImage];
        }
        weakSelf.addButton.image = photo;
        weakSelf.imageUrl = nil;
        weakSelf.addbtn.hidden = NO;
    }else
    {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"相册数据异常")];
    }
}

- (SQCustomButton *)addButton{
    if (!_addButton) {
        _addButton = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(130, 170) midmargin:20];
        _addButton.image = SQGetImage(@"tianjiatupian");
        _addButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
        _addButton.SQBackimageView.contentMode = UIViewContentModeScaleAspectFill;
        _addButton.imageView.clipsToBounds = YES;
        _addButton.imageView.layer.cornerRadius = 10;
        _addButton.imageView.layer.masksToBounds = YES;
        _addButton.text = SQNSLocalString(@"点击拍摄本人真实照片");
        _addButton.font = FONT_REGULAR(12);
        _addButton.textColor = SQColor666666;
    }
    return _addButton;
}

- (UIImagePickerController *)imagePickerVc{
    if (!_imagePickerVc) {
        _imagePickerVc = [[UIImagePickerController alloc] init];
        _imagePickerVc.delegate = self;
        _imagePickerVc.allowsEditing = YES;
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
        if ([UIImagePickerController isSourceTypeAvailable: sourceType]) {
            _imagePickerVc.sourceType = sourceType;
            if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront])
                {
                _imagePickerVc.cameraDevice = UIImagePickerControllerCameraDeviceFront;
                }
        }
    }
    return _imagePickerVc;
}


@end
